<?php
class Product extends CI_Controller{
	
	
	
	public function __construct(){	
		parent::__construct();
		
		if($this->User_model->is_logged_in() != TRUE){
			$preUrl = $_SERVER['PATH_INFO'];
			$this->session->set_userdata('preUrl',$preUrl);
			redirect('login');
		}
		
		if($this->session->userdata('publish') == NULL || $this->session->userdata('controller') != 'product'){
			$this->session->set_userdata('publish',-1);
			$this->session->set_userdata('controller','product');
		}
	}
	
	public function index($pg = 1){
		
		$page = $pg;
		$rp = 2;
		$limit = 2;
		
		$publish = $this->session->userdata('publish');
		
		$total = $this->Product_model->getNumofProduct($publish);
			
		$data['products'] = $this->Product_model->getProductByOffset(($page - 1) * $rp,$limit,$publish);
		$data['permissions'] = $this->Permission_model->getPermission($this->router->fetch_class());
		$data['pg'] = $this->paging($page, $rp, $total, $limit);
		$data['publish'] = $publish;
		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest"){
			$this->load->view('product/index',$data);
		}
		else{
			$this->template->write_view('content','product/index',$data,TRUE);
			$this->template->load();
		}	
	}

	
	public function changeStatus(){
		
		try {
			$this->session->set_userdata('publish', $this->input->post('publish'));
			echo $this->session->userdata('publish');	
		} catch (Exception $e) {
			echo 'false';
		}
	}
	
	function paging($page,$rp,$total,$limit)
	{
        $limit -= 1;

        $mid = floor($limit/2);
        
        if ($total>$rp)
            $numpages = ceil($total/$rp);
        else
            $numpages = 1;
        
        if ($page>$numpages) $page = $numpages;

            $npage = $page;

        while (($npage-1)>0&&$npage>($page-$mid)&&($npage>0))
            $npage -= 1;
        
        $lastpage = $npage + $limit;
        
        if ($lastpage>$numpages) 
            {
            $npage = $numpages - $limit + 1;
            if ($npage<0) $npage = 1;
            $lastpage = $npage + $limit;
            if ($lastpage>$numpages) $lastpage = $numpages;
            }
        
        while (($lastpage-$npage)<$limit) $npage -= 1;        
        
        if ($npage<1) $npage = 1;
        
        //echo $npage; exit;
        
        $paging['first'] = 1;
        if ($page>1) $paging['prev'] = $page - 1; else $paging['prev'] = 1;
        $paging['start'] = $npage;
        $paging['end'] = $lastpage;
        $paging['page'] = $page;            
        if (($page+1)<$numpages) $paging['next'] = $page + 1; else $paging['next'] = $numpages;
        $paging['last'] = $numpages;
        $paging['total'] = $total;
        $paging['iend'] = $page * $rp;
        $paging['istart'] = ($page * $rp) - $rp + 1;
        
        if (($page * $rp)>$total) $paging['iend'] = $total;
        
        return $paging;    
	}
	
	public function google_map(){
		$this->load->view('product/test');
	}
	
	public function test(){
		/*setlocale(LC_TIME, "C");
		echo strftime("%A");
		setlocale(LC_TIME, "fi_FI");
		echo strftime(" in Finnish is %A,");
		setlocale(LC_TIME, "fr_FR");
		echo strftime(" in French %A and");
		setlocale(LC_TIME, "de_DE");
		echo strftime(" in German %A.\n");*/
		
		/*setlocale(LC_ALL, 'us-US');

		echo strftime("%A");*/
		
		/*$datestring = "Year: %Y Month: %m Day: %d - %h:%i %a";
		$time = time();

		echo mdate($datestring, $time);
		
		echo 'Time Zone: ' . timezones('UTC');
*/		
		/*$now = time();

		echo unix_to_human($now); // U.S. time, no seconds

		echo unix_to_human($now, TRUE, 'us'); // U.S. time with seconds

		echo unix_to_human($now, TRUE, 'eu'); // Euro time with seconds*/
		
		/*
		$anchor = "<a href='/Bong-da-Italia/Index.aspx' class='awdawdwa' >Bong Da Y</a>";
		
		$pos = strpos($anchor, 'href', 1);
		
		$link = '';
		$i = 6;
		while($anchor[$pos + $i] != "'"){
			$link .= $anchor[$pos + $i++];
		}
		
		
		while($anchor[$pos + $i] != ">"){
			$i++;
		}
		
		$i++;
		
		$title = '';
		
		while($anchor[$pos + $i] != "<"){
			$title .= $anchor[$pos + $i++];
		}
		
		echo $link . '<br/>';
		
		echo $title;
		*/
		
		//$ipaddress = $this->input->ip_address();
		$ipaddress = "1.52.224.230";
		
		$_API = "2d130d722781b4a9e104e3c20981454e169cd067af1a9d61f4698a2ead41d38b";
		$_URL = "http://api.ipinfodb.com/v3/ip-city/?key=$_API&ip=".$ipaddress."&format=json";
		
		
		$data = file_get_contents($_URL);
		
		//$data = json_decode($data);
		echo $data;
		/*
		if(isset($data->statusCode) && $data->statusCode == "OK"){
		    if(isset($data->countryCode) && ($data->countryCode != "USA" || $data->countryCode != "BD")){
		        header("Location:http://www.yourdomain.com/geolocation.php");     
		        exit;
		    }
		}
		*/

	}
	
	public function test1(){
		$tags = get_meta_tags('http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=1.52.224.230');
		print $tags['country'];  // city name
	}
	
}